<?php

namespace App\Http\Controllers;

use App\Goods;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->order) {
            return view('pages.goods', [
                'goods' => Goods::with(['kind:id,name', 'craftsman:id,shop_name'])->orderBy('order_count', 'asc')->paginate(10),
                'order' => 'asc',
            ]);
        }

        return view('pages.goods', [
            'goods' => Goods::with(['kind:id,name', 'craftsman:id,shop_name'])->orderBy('order_count', 'desc')->paginate(10),
            'order' => 'desc',
        ]);
    }


    public function destroy(Goods $goods)
    {
        $oldFile = Str::after($goods->image_src, "http://craftsman.test/storage/");
        Storage::delete(Str::finish("public/", $oldFile));
        $goods->delete();
        return back()->withStatus(__('商品移除成功'));
    }
}
